package com.guomei.controller;

import com.guomei.bean.AddMenuDTO;
import com.guomei.bean.DeleteMenuDTO;
import com.guomei.bean.GetAllUserDTO;
import com.guomei.bean.GetMenuListDTO;
import com.guomei.service.menu.MenuService;
import com.guomei.utils.Result;
import com.guomei.utils.ValidationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 菜单管理
 */
@RestController
@RequestMapping("/api1/menu")
@Slf4j
public class MenuController {
    @Autowired
    MenuService menuService;


    @PostMapping("/addMenu")
    public Result<?> addMenu(@Valid @RequestBody AddMenuDTO addMenuDTO, BindingResult result) {
        log.info("添加菜单入参：{}", addMenuDTO);
        //实体类注解参数校验
        Result commonDataResponse = ValidationUtil.validInputParams(result);
        if (commonDataResponse != null) {
            return commonDataResponse;
        }
        try {
            return menuService.addMenu(addMenuDTO);
        } catch (Exception e) {
            log.info("添加菜单异常：{}", e);
            return Result.error("添加菜单失败");
        }
    }

    @PostMapping("/deleteMenu")
    public Result<?> deleteMenu(@Valid @RequestBody DeleteMenuDTO deleteMenuDTO, BindingResult result) {
        log.info("删除菜单入参：{}", deleteMenuDTO);
        //实体类注解参数校验
        Result commonDataResponse = ValidationUtil.validInputParams(result);
        if (commonDataResponse != null) {
            return commonDataResponse;
        }
        try {
            return menuService.deleteMenu(deleteMenuDTO);
        } catch (Exception e) {
            log.info("删除菜单异常：{}", e);
            return Result.error("删除菜单失败");
        }
    }

    @PostMapping("/getMenuList")
    public Result<?> getMenuList(@Valid @RequestBody GetMenuListDTO getMenuListDTO, BindingResult result) {
        log.info("查询菜单入参：{}", getMenuListDTO);
        //实体类注解参数校验
        Result commonDataResponse = ValidationUtil.validInputParams(result);
        if (commonDataResponse != null) {
            return commonDataResponse;
        }
        try {
            return menuService.getMenuList(getMenuListDTO);
        } catch (Exception e) {
            log.info("查询菜单异常：{}", e);
            return Result.error("查询菜单失败");
        }
    }
}
